import Axios from "axios";
import router from "@/router";
import {Message} from "element-ui";

export function request(config) {
    let instance = Axios.create({
        baseURL: "/api"
    });

    //请求拦截
    instance.interceptors.request.use(req => {
        req.headers = {'token': localStorage.getItem("token")}
        return req;
    })

    //响应拦截
    instance.interceptors.response.use(res => {
        if(res.data.code === 400) {
            if(res.data.message === "请求拦截！") {
                Message.error("用户已过期，请重新登录！")
                router.replace("/login").catch(err => err);
            }
        }
        return res.data;
    })

    return instance(config);
}